<?php

namespace app\common\service;


use think\Db;

class HostNormalRuleRelation
{
    /**
     * @name 获取总数
     * @author wx
     * @date 2019/11/12 11:00
     */
    public function total($host_id)
    {
        $return = Db::name('HostNormalRuleRelation')
            ->field('`group_id`,count(id)')
            ->where('host_id', $host_id)
            ->group('group_id')
            ->column('count(*)','group_id');

        return $return;
    }

    /**
     * @name 查询使用数量
     * @param $host_id
     * @return array
     * @author wx
     * @date 2019/11/12 11:21
     */
    public function used($host_id)
    {
        $return = Db::name('HostNormalRuleRelation')
            ->field('`group_id`,count(id)')
            ->where('host_id', $host_id)
            ->where('status', 1)
            ->group('group_id')
            ->column('count(*)','group_id');

        return $return;
    }

    /**
     * @name 修改
     * @param $condition
     * @param $updateData
     * @return int|string
     * @author wx
     * @date 2019/11/12 14:20
     */
    public function modify($condition, $updateData)
    {
        return Db::name('host_normal_rule_relation')->where($condition)->update($updateData);
    }

    /**
     * @name 查询
     * @param $condition
     * @return array|null|\PDOStatement|string|\think\Model
     * @author wx
     * @date 2019/11/15 16:02
     */
    public function findData($condition)
    {
        return Db::name('host_normal_rule_relation')->where($condition)->find();
    }

    /**
     * @name 查找关系
     * @param $conditon
     * @param string $field
     * @return array|null|\PDOStatement|string|\think\Model
     * @author wx
     * @date 2020/1/8 19:47
     */
    public function findSidData($condition, $field = '*')
    {

        return Db::name('host_normal_rule_relation')->alias('hn')->
        join('normal_rule nr','nr.id = hn.rule_id')
            ->where($condition)->field($field)->find();

    }

    public function findStatus($condition)
    {
        $status = Db::name('host_normal_rule_relation')->where($condition)->value('status');
        $return = empty($status) ? 0 : 1;
        return $return;
    }

    /**
     * @name: 添加
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-09 17:03:06
     */
    public function add ($host_id)
    {
        if(Db::name('HostNormalRuleRelation')->where('host_id', $host_id)->count() > 0)
        {
            return [];
        }
        Db::query('INSERT INTO `' . config('database.prefix') . 'host_normal_rule_relation` (`rule_id`,`host_id`,`group_id`,`status`,`handle`)  select `id`,' . $host_id . ',`group_id`,`default`,`handle` from `' . config('database.prefix') . 'normal_rule`');
    }

    /**
     * @name: 删除
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-24 13:19:24
     */
    public function del ($host_id_a)
    {
        Db::name('HostNormalRuleRelation')->whereIn('host_id', $host_id_a)->delete();
    }
}